<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/LocaleContainer.class.php";
require_once dirname(__FILE__)."/SessionRoster.class.php";
require_once dirname(__FILE__)."/Client.class.php";
require_once dirname(__FILE__)."/GracePeriodManager.class.php";

class Config
{	
	static private $instance;
	static private $has_config;
	
	private $sql_server;
	private $sql_database;
	private $sql_user;
	private $sql_password;
	
	private $roster_title;
	private $roster_server;
	private $roster_password;
	private $roster_logo;
	
	private $admin_password;
	
	private $guilds;
	
	private $database_armory;
	
	private $localization;
	
	private $uploader_allowed;
	
	private $grace_period;
	
	private function __construct()
	{
		$locale_container = LocaleContainer::GetInstance();
		
		$this->sql_server = null;
		$this->sql_database = null;
		$this->sql_user = null;
		$this->sql_password = null;
		
		$this->roster_title = "Broadcast your skills!";
		$this->roster_server = null;
		$this->roster_password = null;
		$this->roster_logo = null;
		
		$this->guilds = array();
		
		$this->database_armory = "http://www.wowarmory.com/";
		$this->database_items = "http://www.wowarmory.com/item-info.xml?i=%s";
		$this->database_spells = "http://www.wowhead.com/?spell=%s";
		
		$this->localization = $locale_container->GetDefaultLocale();
		
		$this->uploader_allowed = false;
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			$t_config = @unserialize( file_get_contents( dirname(__FILE__)."/../class.data/Config.class.serialize" ) );
			
			if ( $t_config == FALSE )
			{
				$client = Client::GetInstance();
				self::$has_config = false;
				self::$instance = new Config();
				
				if ( $client->GetCurrentPage() != "install.php" and $client->GetCurrentPage() != "change_language.php" )
				{
					$session_roster = SessionRoster::GetInstance();

					$session_roster->Redirect("install.php");
				}
			}
			else
			{
				self::$instance = $t_config;
				self::$has_config = true;
			}
		}
		
		return self::$instance;
	}
	
	function Save()
	{
		if ( @file_put_contents( dirname(__FILE__)."/../class.data/Config.class.serialize", serialize($this) ) == FALSE )
		{
			return false;
		}
		
		return true;
	}
	
	function GetRosterTitle()
	{
		return $this->roster_title;
	}
	
	function HasRosterTitle()
	{
		if ($this->roster_title != null)
		{
			return true;
		}
		
		return false;
	}
	
	function HasConfig()
	{
		return self::$has_config;
	}
	
	function GetLocalization()
	{
		return $this->localization;
	}
	
	function GetSqlServer()
	{
		return $this->sql_server;
	}
	
	function GetSqlDatabase()
	{
		return $this->sql_database;
	}
	
	function GetSqlUser()
	{
		return $this->sql_user;
	}
	
	function GetSqlPassword()
	{
		return $this->sql_password;
	}
	
	function GetRosterServer()
	{
		return $this->roster_server;
	}
	
	function GetRosterServerRawWithSlashes()
	{
		$roster_server = html_entity_decode($this->roster_server, ENT_QUOTES);
		$roster_server = addslashes($roster_server);
		
		return $roster_server;
	}
	
	function GetRosterPassword()
	{
		return $this->roster_password;
	}
	
	function GetAdminPassword()
	{
		return $this->admin_password;
	}
	
	function GetRosterBanner()
	{
		return $this->roster_logo;
	}
	
	function GetRosterBannerUrl()
	{
		if ($this->roster_logo == null)
		{
			return "img/lzroster.png";
		}
		else
		{
			return "upload/".$this->roster_logo;
		}
	}
	
	function GetRosterGuilds()
	{
		return $this->guilds;
	}
	
	function GetRosterGuildsRawWithSlashes()
	{
		$t_guilds = array();
		
		foreach ($this->guilds as $guild_name)
		{
			$guild_name = html_entity_decode($guild_name, ENT_QUOTES);
			$guild_name = addslashes($guild_name);
			
			array_push($t_guilds, $guild_name);
		}
		
		return $t_guilds;
	}
	
	function GetWoWArmory()
	{
		return $this->database_armory;
	}
	
	/**
	 * Returns the grace period object related
	 * to this config or a default one if none
	 * is defined.
	 */
	function GetGracePeriod() {
		return GracePeriodManager::GetInstance()->GetGracePeriodById($this->grace_period);
	}
	
	/**
	 * Sets the id of the grace period to use
	 * on the roster.
	 */ 
	function SetGracePeriod($grace_period) {
		$this->grace_period = $grace_period;
	}
	
    function IsUploaderAllowed()
    {
        return $this->uploader_allowed;
    }
	
	function SetSqlServer($value)
	{
		$this->sql_server = $value;
	}
	
	function SetSqlPassword($value)
	{
		$this->sql_password = $value;
	}
	
	function SetSqlDatabase($value)
	{
		$this->sql_database = $value;
	}
	
	function SetSqlUser($value)
	{
		$this->sql_user = $value;
	}
	
	function SetRosterTitle($value)
	{
		$this->roster_title = $value;
	}
	
	function SetRosterServer($value)
	{
		$this->roster_server = $value;
	}
	
	function SetAdminPassword($value)
	{
		$this->admin_password = $value;
	}
	
	function SetRosterPassword($value)
	{
		$this->roster_password = $value;
	}
	
	function SetRosterGuilds($guilds)
	{
		if ( preg_match_all("/ *([^,]+) */", $guilds, $matches) == FALSE )
		{
			return false;
		}
		
		if ( count($matches[1]) < 1 )
		{
			return false;
		}
		
		$this->guilds = $matches[1];
		
		return true;
	}
	
	function SetRosterBanner($value)
	{
		$this->roster_logo = $value;
	}
	
	function SetWoWArmory($value)
	{
		$this->database_armory = $value;
	}
	
	function SetItemDatabase($value)
	{
		$this->database_items = $value;
	}
	
	function SetSpellDatabase($value)
	{
		$this->database_spells = $value;
	}
	
	function SetLocalization($value)
	{
		$locale_container = LocaleContainer::GetInstance();
		
		if ($locale_container->HasLocale($value) == false)
		{
			return false;
		}
		else
		{
			$this->localization = $value;
			return true;
		}
	}

	function SetUploaderAllowed($value)
	{
	    $this->uploader_allowed = $value;
	}
}

?>